﻿using System.Collections.Generic;
using System.Linq;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.BLL
{
    public partial class CellPhoneInsuranceInfoService
    {
        /// <summary>
        /// 创建保单与服务项目的映射（即快照）
        /// </summary>
        /// <param name="insuranceOrderID"></param>
        /// <param name="insuranceProductID"></param>
        /// <param name="cellPhoneInsuranceInfoList">填写的相关信息，根据产品的服务项目个数，填写多个IMEI、IDFV、ProductTypeName等</param>
        /// <returns></returns>
        public bool CreateSnapshoot(int insuranceOrderID, int insuranceProductID, List<CellPhoneInsuranceInfo> cellPhoneInsuranceInfoList)
        {
            var cellPhoneServiceProjectList =
                ServiceFactory.CellPhoneServiceProjectServiceInstance.GetProductCellPhoneServiceProjectList(
                    insuranceProductID);
            var result = true;
            if (cellPhoneServiceProjectList.Any() && cellPhoneInsuranceInfoList.Any() && cellPhoneServiceProjectList.Count == cellPhoneInsuranceInfoList.Count)
            {
                for (int i = 0; i < cellPhoneServiceProjectList.Count; i++)
                {
                    if (!result)
                        return false;
                    var cellPhoneServiceProject = cellPhoneServiceProjectList[i];
                    var cellPhoneInsuranceInfoModel = cellPhoneInsuranceInfoList[i];
                    var cellPhoneInsuranceInfo = new CellPhoneInsuranceInfo
                    {
                        InsuranceOrderID = insuranceOrderID,
                        CellPhoneServiceProjectID = cellPhoneServiceProject.ID,
                        ServiceProjectId = cellPhoneServiceProject.ServiceProjectId,
                        ServiceProjectName = cellPhoneServiceProject.ServiceProjectName,
                        #region 需用户填写的属性
                        IMEI = cellPhoneInsuranceInfoModel.IMEI,
                        IDFV = cellPhoneInsuranceInfoModel.IDFV,
                        ProductTypeId = cellPhoneInsuranceInfoModel.ProductTypeId,
                        ProductTypeName = cellPhoneInsuranceInfoModel.ProductTypeName,
                        ProductBrandId = cellPhoneInsuranceInfoModel.ProductBrandId,
                        ProductBrandName = cellPhoneInsuranceInfoModel.ProductBrandName,
                        ProductModelId = cellPhoneInsuranceInfoModel.ProductModelId,
                        ProductModelName = cellPhoneInsuranceInfoModel.ProductModelName,
                        #endregion
                        DeviceType = cellPhoneServiceProject.InsuranceDeviceType,
                        NewMachineInsuranceTimeLimit = cellPhoneServiceProject.NewMachineInsuranceTimeLimit,
                        ServiceTimes = cellPhoneServiceProject.ServiceTimes,
                    };
                    result = AddEntities(cellPhoneInsuranceInfo).ID > 0;
                }
            }
            return result;
        }

        public IQueryable<CellPhoneInsuranceInfo> Load(List<long> cellPhoneInsuranceInfoIDs)
        {
            return LoadEntities(o => cellPhoneInsuranceInfoIDs.Contains(o.ID));
        }

        public IQueryable<CellPhoneInsuranceInfo> LoadByInsuranceID(long insuranceOrderID)
        {
            return LoadEntities(o => o.InsuranceOrderID == insuranceOrderID && o.CellPhoneServiceProjectID.HasValue);
        }

        public CellPhoneInsuranceInfo Load(long insuranceOrderID, int cellPhoneServiceProjectID)
        {
            return LoadEntities(o => o.InsuranceOrderID == insuranceOrderID && o.CellPhoneServiceProjectID == cellPhoneServiceProjectID).FirstOrDefault();
        }
        public CellPhoneInsuranceInfo Load(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }

    }
}